package com.tricycle.scada.mqtt.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tricycle.scada.mqtt.entity.RealtimeValueEntity;
import com.tricycle.scada.mqtt.mapper.RealtimeValueMapper;
import com.tricycle.scada.mqtt.model.RealtimeValueModel;
import com.tricycle.scada.mqtt.service.RealtimeValueService;
import io.netty.util.concurrent.CompleteFuture;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

@Service
public class RealtimeValueServiceImpl extends ServiceImpl<RealtimeValueMapper, RealtimeValueEntity> implements RealtimeValueService {

    private RedisTemplate<String, String> redisTemplate;

    public RealtimeValueServiceImpl(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Async("taskExecutor")
    public CompletableFuture<Set<String>> getValues(String tag, Double start, Double end) {
            if (start == null && end == null) {
                return CompletableFuture.completedFuture(redisTemplate.opsForZSet().range(tag, 0, 0));
            } else {
                if (start == null) {
                    start = Double.NEGATIVE_INFINITY;
                }
                if (end == null) {
                    end = Double.POSITIVE_INFINITY;
                }
                return CompletableFuture.completedFuture(redisTemplate.opsForZSet().rangeByScore(tag, start, end));
            }
    }
}
